package com.huadao.pda.data.http;

import android.accounts.NetworkErrorException;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.huadao.pda.common.RockToast;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

public abstract class RockBaseObserver<T> extends BaseObserver<T> {

    private static final String TAG="RockBaseObserver";

    private boolean isNeedProgress;
    private String loadingMsg;

//    private ExceptionInfoDao exceptionInfoDao;
//    private AsyncSession asyncSession;

    public RockBaseObserver() {
        this(null,null);
    }

    public RockBaseObserver(BaseImpl mBaseImpl) {
        this(mBaseImpl,null);
    }

    public RockBaseObserver(BaseImpl mBaseImpl, String loadingMsg) {
        super(mBaseImpl);
        this.loadingMsg = loadingMsg;
        if (TextUtils.isEmpty(loadingMsg)) {
            this.isNeedProgress = false;
        } else {
            this.isNeedProgress = true;
        }
//        asyncSession=GreenDaoManager.getInstance().getAsyncNoCacheSession();
    }

    @Override
    protected void onBaseError(Throwable t) {
        StringBuilder sb=new StringBuilder();
//        sb.append("请求失败：");
        if(t instanceof NetworkErrorException || t instanceof UnknownHostException || t instanceof ConnectException){
            sb.append("网络异常");
        }else if(t instanceof SocketTimeoutException ||t instanceof InterruptedIOException || t instanceof TimeoutException){
            sb.append("请求超时");
        }
//        else if(t instanceof JsonSyntaxException){
//            sb.append("请求不合法");
////            exceptionCode=ExceptionCode.NETWORK_JSON;
//        }
        else if(t instanceof JsonSyntaxException || t instanceof JsonParseException || t instanceof JSONException || t instanceof ParseException){
            sb.append("解析错误");
        }else if(t instanceof ApiException){
//            if(((ApiException) t).isTokenExpried()){
//                sb.append("Token 出错");
//            }else {
//                sb.append(t.getLocalizedMessage());
//            }
            sb.append(t.getLocalizedMessage());
        }else {
            sb.append(t.getLocalizedMessage());
        }
        if (t.getLocalizedMessage().equals("The mapper function returned a null value.")) {
            //服务器返回data为null
            onResponseNullData();
        }else {
            RockToast.showToastSafe(sb.toString());
            Log.e(TAG, "onBaseError: " + sb.toString());
        }

    }

    protected void onResponseNullData() {
        Log.e(TAG, "onResponseNullData");
    }

    @Override
    protected boolean isNeedProgressDialog() {
        return isNeedProgress;
    }

    @Override
    protected String getLoadingMsg() {
        return loadingMsg;
    }
}
